#include "at89x51.h"

sbit ir=P2^0;
char i=0;
char j=0;
char dat[4]={0};

void usart_init()
{
	TMOD=0x21;
	SCON=0x50;
	TH1=0xfd;
	TL1=0xfd;
	TR1=1;
	TI=1;
}

void usart_txd(char dat)
{
	EA=0;
	while(!TI);
	TI=0;
	SBUF=dat;
	EA=1;
} 

void t0() interrupt 1
{ 
	TR0=0;
}

void main()
{
	ET0=1;
	EA=1;
	usart_init();

	while(1)
	{
		while(ir);
		TH0=0xdf;TL0=0x99;
		TR0=1;
		while(!TF0);
		while(!ir);
		if(ir)
		{
			TH0=0xef;TL0=0xcc;
			TR0=1;
			while(!TF0);
			while(ir);
			if(!ir)
			{
				for(i=0;i<4;i++)
				{
					for(j=0;j<8;j++)
					{
						TH0=0xfb;TL0=0xf0;
						TR0=1;
						while(!TF0);
						dat[i]<<=1;
						if(ir)
						{
							dat[i]|=1;
						}
						while(ir);
					}
				}
			}
			for(i=0;i<4;i++)
			{
				usart_txd(dat[i]);
				dat[i]=0;
			}
			TH0=0x10;TL0=0x00;
			TR0=1;
			while(!TF0);
		}
	}
}